<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\BasePostLabelsModel;
use app\model\PostLabelsModel;
use app\model\PostWhiteUserModel;
use app\model\UserModel;
use think\App;
use think\console\command\make\Model;

class PostWhiteUser extends Base {

    protected $middleware = [Auth::class];

    private $model;

    private $req;

    private $aid;

    private $page;

    private $limit;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->model = new PostWhiteUserModel();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 10;

        $this->aid = $this->isLogin();
    }



    public function getList(){

        $where=[];

        $list = $this->model->getList($where,$this->page,$this->limit,["updated_at"=>"DESC"]);

        $total = $this->model->getTotal($where);

        return $this->succData($total,$list);

    }


    //新增
    public function add(){

        if(!$this->req["yx"]){
            return $this->fail("予心号不能为空");
        }

        if($this->model->getTotal(["yx"=>$this->req["yx"]]) > 0){
            return $this->fail("该予心号已经存在");
        }

        $user = (new UserModel())->getUserCacheByYx($this->req["yx"]);

        if(empty($user)){
            return $this->fail("该予心号不存在");
        }

        $res = $this->model->addOne(["yx"=>$this->req["yx"],'uid'=>$user['uid']]);

        if($res){
            return $this->succ("新增成功");
        }else{
            return $this->fail("新增失败");
        }
    }


    public function edit(){

        if(!$this->req["yx"]){
            return $this->fail("予心号不能为空");
        }

        $where =[] ;
        $where[] = ["yx","=",$this->req["yx"]];
        $where[] = ["id","<>",$this->req["id"]];
        if($this->model->getTotal($where) > 0){
            return $this->fail("该予心号已经存在");
        }

        $user = (new UserModel())->getUserCacheByYx($this->req["yx"]);

        if(empty($user)){
            return $this->fail("该予心号不存在");
        }

        $res = $this->model->updateOne($this->req["id"],["yx"=>$this->req["yx"]]);

        if($res){
            return $this->succ("修改成功");
        }else{
            return $this->fail("修改失败");
        }
    }


    public function del(){

        if(!$this->req["id"]){
            return $this->fail("请选择要操作的数据");
        }

        $res = $this->model->where("id",'=',$this->req["id"])->delete();

        if($res){
            return $this->succ("删除成功");
        }else{
            return $this->fail("删除失败");
        }
    }

}
